package com.wenhai.leetcode.linkedList;

/**
 * https://leetcode-cn.com/problems/linked-list-cycle/
 *
 * @author wenhai
 * @date   2021/4/7
 */
public class _141环形链表 {


    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode fast = head.next.next;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            if (slow.val == fast.val) {
                return true;
            }
            slow =slow.next;
            fast = fast.next.next;
        }
        return false;
    }


    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

}
